﻿using System.Linq;
using System.Security.Principal;
using AutoClub.Core.Domain;
using AutoClub.Core.Domain.Dao;
using Microsoft.Practices.ServiceLocation;

namespace AutoClub.Support
{
    /// <summary>
    /// Extensions methods for principal interface
    /// </summary>
    public static class IPrincipalExtensions
    {
        /// <summary>
        /// Returns true if the actual user is an administrator.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool IsAdmin(this IPrincipal user)
        {
            var userRepo = ServiceLocator.Current.GetInstance<IUsersRepo>();

            User actual = userRepo.GetAllAdministrators()
                            .SingleOrDefault(u => u.Email.Equals(user.Identity.Name));

            return actual != null;
        }
    }
}